package com.huanglj.himalaya.adapters;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.huanglj.himalaya.R;
import com.huanglj.himalaya.utils.LogUtil;
import com.huanglj.himalaya.views.PobWindow;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.util.ArrayList;
import java.util.List;

public class PlayListAdapter extends RecyclerView.Adapter<PlayListAdapter.InnerHolder> {
    private static final String TAG = "PlayListAdapter";
    private int playingIndex = 0;

    private List<Track> mTrackList = new ArrayList<>();
    private PobWindow.OnItemClickListener mListener;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_play_list, parent, false);
        return new InnerHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, final int position) {
        TextView playTitle = holder.itemView.findViewById(R.id.play_list_title);
        String trackTitle = mTrackList.get(position).getTrackTitle();
        playTitle.setText(trackTitle);
        ImageView playingIconView = holder.itemView.findViewById(R.id.play_icon_iv);
        //设置当前的position变成显示控件
        playingIconView.setVisibility(playingIndex == position ? View.VISIBLE: View.GONE);

        playTitle.setTextColor(holder.itemView.getContext().getResources().getColor(playingIndex ==
                position ? R.color.detail_color : R.color.play_list_color));

        playTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onItemClick(position);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mTrackList.size();
    }

    public void setListData(List<Track> tracks) {
        mTrackList.clear();
        mTrackList.addAll(tracks);
        notifyDataSetChanged();
    }

    public void setPosition(int position) {
        LogUtil.d(TAG,"position:" + position);
        playingIndex = position;
        notifyDataSetChanged();
    }

    public void setOnItemClick(PobWindow.OnItemClickListener listener) {
        mListener = listener;
    }

    public class InnerHolder extends RecyclerView.ViewHolder {
        public InnerHolder(@NonNull View itemView) {
            super(itemView);
        }
    }


}
